home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / dviware / umddvi / dev / dmdhost.c < prev    next >
C/C++ Source or Header  |  1990-10-01  |  4KB  |  237 lines

  1.  
  2. #include <stdio.h>
  3. #ifdef    sys5
  4. #include <sys/termio.h>
  5. #else
  6. #include <sgtty.h>
  7. #endif
  8. #include <signal.h>
  9. #include <sys/jioctl.h>
  10. #include <setjmp.h>
  11. #include "dmdcodes.h"
  12.  
  13. static jmp_buf jenv;
  14. int jerq;
  15. static char prooftty[] = "/dev/tty";
  16. #ifdef    sys5
  17. static struct termio sttybuf, sttysave;
  18. #else
  19. static struct sgttyb modes, savetty;
  20. #endif
  21. static int termraw;
  22.  
  23. void
  24. sighup()
  25. {
  26.     putchar(DMD_EXIT);
  27.     exit(1);
  28. }
  29.  
  30. dmdstart()
  31. {
  32.     char command[256];
  33.     int ismpx;
  34.  
  35.     signal(SIGHUP, sighup);
  36.  
  37.     if ((jerq = open(prooftty, 2)) < 0) {
  38.         error(1, 0, prooftty);
  39.         exit(1);
  40.     }
  41.     if (ioctl(jerq, JMPX, 0) == -1)
  42.         ismpx = 0;
  43.     else
  44.         ismpx = 1;
  45.  
  46. #ifdef sys5
  47.     ioctl(jerq, TCGETA, &sttysave);
  48.     sttybuf.c_iflag = IGNBRK;
  49.     sttybuf.c_cflag = (sttysave.c_cflag & (CBAUD | CLOCAL)) | CS8 | CREAD;
  50.     sttybuf.c_cc[VMIN] = 1;
  51.     (void)ioctl(jerq, TCSETAW, &sttybuf);
  52. #else
  53.     ioctl(jerq, TIOCGETP, &modes);
  54.     savetty = modes;
  55.     modes.sg_flags |= RAW;
  56.     modes.sg_flags &= ~ECHO;
  57.     ioctl(jerq, TIOCSETP, &modes);
  58. #endif
  59.     termraw++;
  60. #define    Proofm    "/usr/local/lib/dvidmd.m"
  61. #define    Proofj    "/usr/local/lib/dvidmd.j"
  62. #define    X32ld    "32ld"
  63.     if (!isatty(1) || !verify()) {
  64.         sprintf(command, "%s %s < %s > %s", X32ld,
  65.             ismpx ? Proofm : Proofj, prooftty, prooftty);
  66.         if (system(command) != 0)
  67.             error(1, 0, "%s failed", X32ld);
  68.         if (!verify())
  69.             error(1, 0, "could not sync display");
  70.     }
  71. }
  72.  
  73. void
  74. alarmcatch(sig)
  75. {
  76.     longjmp(jenv, 1);
  77. }
  78.  
  79. verify()
  80. {
  81.     char c;
  82.  
  83.     signal(SIGALRM, alarmcatch);
  84.     c = DMD_TYPESET;
  85.     write(jerq, &c, 1);
  86.     if (setjmp(jenv))
  87.         return(0);
  88.     alarm(2);
  89.     read(jerq, &c, 1);
  90.     alarm(0);
  91.     return (c == DMD_ACK);
  92. }
  93.  
  94. pagecmd()
  95. {
  96.     int i, c;
  97.  
  98.     switch (c = inkbd()) {
  99.     case DMD_EXIT:
  100.         exit(0);
  101.     case DMD_PAGE:
  102.         i = inkbd();
  103.         if (i > 127)
  104.             i -= 256;
  105.         return(i);
  106.     default:
  107.         error(1, 0, "bad pagecmd response 0%o", c);
  108.     }
  109.     /*NOTREACHED*/
  110. }
  111.  
  112. inkbd()
  113. {
  114.     char c;
  115.     register i;
  116.  
  117.     fflush(stdout);
  118.     if (read(jerq, &c, 1) != 1)
  119.         c = 4;        /* ^D, looks like EOF */
  120.     i = c & 0377;
  121.     return(i);
  122. }
  123.  
  124. exit(n)
  125. {
  126.     fflush(stdout);
  127.     fflush(stderr);
  128.     if (termraw) {
  129. #ifdef    sys5
  130.         (void)ioctl(jerq, TCSETAW, &sttysave);
  131. #else
  132.         ioctl(jerq, TIOCSETP, &savetty);
  133. #endif
  134.     }
  135.     _exit(n);
  136. }
  137.  
  138. #ifndef lint
  139. static char rcsid[] = "$Header: error.c,v 2.5 86/11/08 17:09:39 chris Exp $";
  140. #endif
  141.  
  142. /*
  143.  * Print an error message with an optional system error number, and
  144.  * optionally quit.
  145.  *
  146.  * THIS CODE IS SYSTEM DEPENDENT UNLESS varargs WORKS WITH vprintf
  147.  * OR _doprnt.  It should work properly under System V using vprintf.
  148.  * (If you have vprintf, define HAVE_VPRINTF.)
  149.  */
  150.  
  151. #include <varargs.h>
  152.  
  153. #ifdef lint
  154.  
  155. /* VARARGS3 ARGSUSED */
  156. error(quit, e, fmt) int quit, e; char *fmt; {;}
  157.  
  158. /* VARARGS1 ARGSUSED */
  159. panic(fmt) char *fmt; { exit(1); /* NOTREACHED */ }
  160.  
  161. #else lint
  162.  
  163. extern char *ProgName;
  164. extern int errno;
  165. extern char *sys_errlist[];
  166. extern int sys_nerr;
  167.  
  168. /*
  169.  * We can be civlised by calling legitimate routines.
  170.  */
  171. error(va_alist)
  172.     va_dcl
  173. {
  174.     va_list l;
  175.     int quit, e;
  176.     char *fmt;
  177.  
  178.     if (termraw)
  179.         putchar(DMD_ASCII);
  180.     (void) fflush(stdout);    /* sync error messages */
  181.     (void) fprintf(stderr, "%s: ", ProgName);
  182.     va_start(l);
  183.     /* pick up the constant arguments: quit, errno, printf format */
  184.     quit = va_arg(l, int);
  185.     e = va_arg(l, int);
  186.     if (e < 0)
  187.         e = errno;
  188.     fmt = va_arg(l, char *);
  189. #if defined(sys5) || defined(HAVE_VPRINTF)
  190.     (void) vfprintf(stderr, fmt, l);
  191. #else
  192.     _doprnt(fmt, l, stderr);
  193. #endif
  194.     va_end(l);
  195.     if (e) {
  196.         if (e < sys_nerr)
  197.             (void) fprintf(stderr, ": %s", sys_errlist[e]);
  198.         else
  199.             (void) fprintf(stderr, ": Unknown error code %d", e);
  200.     }
  201.     (void) putc('\n', stderr);
  202.     (void) fflush(stderr);    /* just in case */
  203.     if (termraw)
  204.         putchar(0);
  205.     if (quit) {
  206.         if (termraw)
  207.             putchar(DMD_TERM);
  208.         exit(quit);
  209.     }
  210. }
  211.  
  212. panic(va_alist)
  213.     va_dcl
  214. {
  215.     va_list l;
  216.     char *fmt;
  217.  
  218.     if (termraw)
  219.         putchar(DMD_TERM);
  220.     (void) fflush(stdout);
  221.     (void) fprintf(stderr, "%s: panic: ", ProgName);
  222.     va_start(l);
  223.     /* pick up the constant argument: printf format */
  224.     fmt = va_arg(l, char *);
  225. #if defined(sys5) || defined(HAVE_VPRINTF)
  226.     (void) vfprintf(stderr, fmt, l);
  227. #else
  228.     _doprnt(fmt, l, stderr);
  229. #endif
  230.     va_end(l);
  231.     (void) putc('\n', stderr);
  232.     (void) fflush(stderr);
  233.     abort();
  234. }
  235.  
  236. #endif /* lint */
  237.